home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / rpc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-17  |  2.2 KB  |  128 lines

  1. /*
  2.   rpc.c
  3.  
  4.   Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  5.   
  6.   $Id: rpc.c,v 1.3 2000/05/17 16:05:09 dugsong Exp $
  7. */
  8.  
  9. #include <sys/types.h>
  10. #include <rpc/rpc.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "decode.h"
  14. #include "rpc.h"
  15.  
  16. #define FRAGLEN(x)    (x & 0x7fffffff)
  17. #define LASTFRAG(x)    (x & (1 << 31))
  18.  
  19. #define XIDMAPSIZE    64
  20.  
  21. static struct xid_map    xid_maps[XIDMAPSIZE];
  22.  
  23. static int        xid_map_next = 0;
  24. static int        xid_map_hint = 0;
  25.  
  26. /* xid_map adapted from tcpdump's print-nfs.c */
  27.  
  28. void
  29. xid_map_enter(u_long xid, u_long prog, void *data)
  30. {
  31.     struct xid_map *mp;
  32.     
  33.     mp = &xid_maps[xid_map_next];
  34.     
  35.     if (++xid_map_next >= XIDMAPSIZE)
  36.         xid_map_next = 0;
  37.     
  38.     mp->xid = xid;
  39.     mp->prog = prog;
  40.     mp->data = data;
  41. }
  42.  
  43. struct xid_map *
  44. xid_map_find(int xid)
  45. {
  46.     struct xid_map *mp;
  47.     int i;
  48.     
  49.     /* Start searching from where we last left off. */
  50.     i = xid_map_hint;
  51.     do {
  52.         mp = &xid_maps[i];
  53.         if (mp->xid == xid) {
  54.             /* match */
  55.             xid_map_hint = i;
  56.             return (mp);
  57.         }
  58.         if (++i >= XIDMAPSIZE)
  59.             i = 0;
  60.     } while (i != xid_map_hint);
  61.     
  62.     return (NULL);
  63. }
  64.  
  65. int
  66. rpc_decode(u_char *buf, int len, struct rpc_msg *msg)
  67. {
  68.     XDR xdrs;
  69.     u_long *p, fraghdr;
  70.     u_char *tmp;
  71.     int stat, tmplen;
  72.  
  73.     if (len < 20)
  74.         return (0);
  75.     
  76.     p = (u_long *)&buf[4];
  77.  
  78.     /* If not recognizably RPC, try TCP record defragmentation */
  79.     if (ntohl(*p) != CALL && ntohl(*p) != REPLY) {
  80.         tmp = buf;
  81.         tmplen = 0;
  82.         
  83.         for (;;) {
  84.             fraghdr = ntohl(*(u_long *)tmp);
  85.             
  86.             if (FRAGLEN(fraghdr) + 4 > len)
  87.                 return (0);
  88.             
  89.             len -= 4;
  90.             memmove(tmp, tmp + 4, len);
  91.             tmplen += FRAGLEN(fraghdr);
  92.             
  93.             if (LASTFRAG(fraghdr))
  94.                 break;
  95.             
  96.             tmp += FRAGLEN(fraghdr);
  97.             len -= FRAGLEN(fraghdr);
  98.             
  99.             if (len < 4)
  100.                 return (0);
  101.         }
  102.         len = tmplen;
  103.     }
  104.     /* Decode RPC message. */
  105.     if (ntohl(((struct rpc_msg *)buf)->rm_direction) == CALL) {
  106.         xdrmem_create(&xdrs, buf, len, XDR_DECODE);
  107.         
  108.         if (!xdr_callmsg(&xdrs, msg)) {
  109.             xdr_destroy(&xdrs);
  110.             return (0);
  111.         }
  112.     }
  113.     else if (ntohl(((struct rpc_msg *)buf)->rm_direction) == REPLY) {
  114.         msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
  115.         xdrmem_create(&xdrs, buf, len, XDR_DECODE);
  116.         
  117.         if (!xdr_replymsg(&xdrs, msg)) {
  118.             xdr_destroy(&xdrs);
  119.             return (0);
  120.         }
  121.     }
  122.     stat = xdr_getpos(&xdrs);
  123.     xdr_destroy(&xdrs);
  124.     
  125.     return (stat);
  126. }
  127.  
  128.